package com.sr.recitewords.service.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sr.recitewords.common.enums.StateValueEnum;
import com.sr.recitewords.common.enums.TableNamePrefixEnum;
import com.sr.recitewords.datasource.entities.SysUser;
import com.sr.recitewords.datasource.mappers.SysUserMapper;
import com.sr.recitewords.datasource.vo.SysUserVo;
import com.sr.recitewords.utils.IdentifierGeneratorUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Override
    public boolean ynExistByLoginAccount(String loginAccount) {
        long count = count(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getLoginAccount, loginAccount).eq(SysUser::getYnDel, StateValueEnum.ZERO.getValue()));
        return count == 0;
    }

    public SysUser getUserByLoginAccount(String loginAccount) {
        return getOne(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getLoginAccount, loginAccount)
                .eq(SysUser::getYnDel, StateValueEnum.ZERO.getValue()));
    }

    @Override
    public SysUserVo getUserVoByLoginAccount(String loginAccount) {
        return null;
    }

    @Override
    public boolean addSysUserVo(SysUserVo sysUserVo) {
        String userId = IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.SYS_USER);
        sysUserVo.setUserId(userId);
        sysUserVo.setCreateDate(new Date());
        sysUserVo.setYnDel(StateValueEnum.ZERO.getValue());
        return save(sysUserVo);
    }

    @Override
    public SysUserVo getSysUserVoByUserId(String userId) {
        SysUserVo sysUserVo = new SysUserVo();
        SysUser sysUser = getById(userId);
        BeanUtil.copyProperties(sysUser, sysUserVo);
        return sysUserVo;
    }


}
